package com.vite.system.service.impl;


import com.vite.common.constant.CommonConstants;
import com.vite.common.core.domain.entity.SysNotice;
import com.vite.common.core.domain.entity.vo.NoticeUnReadVo;
import com.vite.common.utils.SecurityUtils;
import com.vite.system.domain.NoticeReadRecord;
import com.vite.system.mapper.NoticeReadRecordMapper;
import com.vite.system.mapper.SysNoticeMapper;
import com.vite.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公告 服务层实现
 * 
 * @author vite
 */
@Service
@RequiredArgsConstructor
public class SysNoticeServiceImpl implements ISysNoticeService
{
    private final SysNoticeMapper noticeMapper;

    private final NoticeReadRecordMapper noticeReadRecordMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId, Long userId)
    {
        SysNotice sysNotice = noticeMapper.selectNoticeById(noticeId,userId);
        if(CommonConstants.NOTICE_READ_STATUS.READ_UNDO.equals(sysNotice.getReadStatus())){
            insertRecord(noticeId);
        }
        return sysNotice;
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice, SecurityUtils.getUserId());
    }

    /**
     * 获取未读数量
     */
    @Override
    public NoticeUnReadVo getUnReadNumb() {
        Long userId = SecurityUtils.getUserId();
        int tzUnReadNumb = noticeMapper.getUnReadNumb(CommonConstants.NOTICE_TYPE.NOTICE_TZ, userId);
        int ggUnReadNumb = noticeMapper.getUnReadNumb(CommonConstants.NOTICE_TYPE.NOTICE_GG, userId);
        return new NoticeUnReadVo().setTzUnReadNum(tzUnReadNumb).setGgUnReadNum(ggUnReadNumb);
    }

    /**
     * 获得已读用户列表
     */
    @Override
    public List<String> getReadUserNameList(Long noticeId, List<String> userNameList) {
        return noticeReadRecordMapper.getReadUserNameList(noticeId,userNameList);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNotice(SysNotice notice)
    {
        notice.setCreateBy(SecurityUtils.getUsername());
        int row = noticeMapper.insertNotice(notice);
        insertRecord(notice.getNoticeId());
        return row;
    }

    /**
     * 添加已读记录
     */
    private void insertRecord(Long noticeId){
        NoticeReadRecord noticeReadRecord = new NoticeReadRecord();
        noticeReadRecord.setNoticeId(noticeId)
                        .setUserId(SecurityUtils.getUserId())
                        .setUserName(SecurityUtils.getUsername());
        noticeReadRecordMapper.insertNoticeReadRecord(noticeReadRecord);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    /**
     * 重新推送公告
     */
    @Override
    public int rePushNotice(Long noticeId) {
        int row = noticeReadRecordMapper.rePushNotice(noticeId);
        insertRecord(noticeId);
        return row;
    }

}
